/*----------------------------------------------------------------------
  - File name      : tmt.c
  - Author         : Quark Team
  - Update date    : 2020-12-13             
  -	Copyright      : Gevico Electronic studio   
  - Module comments: Task Management Tool module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "main.h"
#include "tmt.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: Task definition                          |
--------------------------------------------------------*/

typedef struct 
{
	uint8 Run;               //Program running flag
  uint16 TIMCount;         //Time counter
	uint16 TRITime;          //Task running interval
	void (*TaskHook) (void); //Task function to run
} TASK_COMPONENTS;       

/*--------------------------------------------------------
| @Description: Task definition                          |
--------------------------------------------------------*/

static TASK_COMPONENTS Task_Comps[TASKS_MAX];

/*--------------------------------------------------------
| @Description: Maximum number of task lists             |
--------------------------------------------------------*/

static uint8 Tasks_Max;

/*--------------------------------------------------------
| @Description: Task execution status                    |
--------------------------------------------------------*/

static uint8 tmt_state = 0; /* Initialize to 0 */

/*--------------------------------------------------------
| @Description: Task number of now                       |
--------------------------------------------------------*/

static uint8 task_num = 0; /* Initialize to 0 */

/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: Task time handler function               |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void TMT_Pro_Time_Callback(void)
{
	uint8 i;
	for(i=0; i<Tasks_Max; i++){
		 /* If time arrives */
		Task_Comps[i].TIMCount--;
	}	
}


/*--------------------------------------------------------
| @Description: Task remarks handler function            |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void TMT_Pro_Handler_Callback(void)
{
	uint8 i;
	for(i=0; i<Tasks_Max; i++){
		 /* If time arrives */
		if(Task_Comps[i].TIMCount == 0 && Task_Comps[i].Run == 1){
			Task_Comps[i].TIMCount = Task_Comps[i].TRITime;
			/* 
				To prevent process conflict, 
				only one process can be executed in the same time period.
			*/
		    Task_Comps[i].TaskHook();  /* Run task */
		}
	}	
}

/*--------------------------------------------------------
| @Description: Task process create function             |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void TMT_Pro_Create(void (*TaskHook) (void),uint16 TRITime)
{	
	Task_Comps[task_num].TaskHook = TaskHook;
	Task_Comps[task_num].TRITime = TRITime;
	Task_Comps[task_num].TIMCount = TRITime;
	Task_Comps[task_num].Run = 1;
    task_num = (task_num + 1) % (TASKS_MAX+1);
	Tasks_Max = task_num;
}

/*--------------------------------------------------------
| @Description: Task process time config function        |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void TMT_Pro_Time_Ctrl(void (*TaskHook) (void),uint16 TRITime)
{	
	uint8 i;
	for(i=0; i<Tasks_Max; i++)    {
		if(Task_Comps[i].TaskHook == TaskHook){
			Task_Comps[i].TIMCount = TRITime;
			Task_Comps[i].TRITime = TRITime;
			return;
		}
	}
}

/*--------------------------------------------------------
| @Description: Task process delete function             |
| @param      : void (*TaskHook) (void)                  |
| @return     : None                                     |
--------------------------------------------------------*/

void TMT_Pro_Delete(void (*TaskHook) (void))
{	
	uint8 i;
	if(Tasks_Max == 0) return;
	for(i=0; i<Tasks_Max; i++) {
		if(Task_Comps[i].TaskHook == TaskHook){
				Task_Comps[i].TaskHook = Task_Comps[Tasks_Max-1].TaskHook;
				Task_Comps[i].TRITime = Task_Comps[Tasks_Max-1].TRITime;
				Task_Comps[i].TIMCount = Task_Comps[Tasks_Max-1].TIMCount;
				Task_Comps[i].Run =Task_Comps[Tasks_Max-1].Run;
				Tasks_Max -= 1;			
			    return;
		}
	}
}


/*--------------------------------------------------------
| @Description: Task process run function                |
| @param      : void (*TaskHook) (void)                  |
| @return     : None                                     |
--------------------------------------------------------*/
void TMT_Pro_Run_Ctrl(void (*TaskHook) (void),PRO_STATE State)
{
	uint8 i;
	for(i=0; i<Tasks_Max; i++) {
		if(Task_Comps[i].TaskHook == TaskHook) {
		    Task_Comps[i].Run = State;
			return;
		}
	}
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/


